package violin;

import org.apache.commons.lang.ClassUtils;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.lang.reflect.Method;

public class StringSplitTest {
    @Test
    public void should_split_string_into_paths() {
        String urlPattern = "/test/{id}";

        assertTrue(isMatch("/test/1", urlPattern));
        assertFalse(isMatch("/test/1/test1", urlPattern));
    }

    @Test
    public void should_extract_path_params_into_map() {
        String urlPattern = "/test/{id}/{name}";

        String url = "/test/1/test1";

        assertThat(isMatch(url, urlPattern), equalTo(true));

        urlPattern.replaceAll("/\\{[^/]+\\}", "/[^/]+?");

        Map pathParams = extractPathParams(url, urlPattern);

        Map expectedParams = new HashMap();
        expectedParams.put("id", "1");
        expectedParams.put("name", "test1");

        assertThat(pathParams, equalTo(expectedParams));
    }

    private Map extractPathParams(String url, String urlPattern) {
        String regex = urlPattern.replaceAll("/\\{[^/]+\\}", "/[^/]+?");
        String[] strings = urlPattern.split("/");
        String[] strings1 = url.split("/");
        Map pathParams = new HashMap();
        for (int i = 0; i < strings.length; i++) {
            String string = strings[i];
            String string1 = strings1[i];
            if (string.matches("\\{[^/]+\\}")) {
                pathParams.put(string.substring(1, string.length() - 1), string1);
            }
        }
        return pathParams;
    }

    private boolean isMatch(String url, String urlPattern) {
        String s = urlPattern.replaceAll("/\\{[^/]+\\}", "/[^/]+?");
        return url.matches(s);
    }

    @Test
    public void should_() {
        String template = "one /{id}/two cats /{name}/ the yard";

        String str = "one /cat/two cats /in/ the yard";

        String regex = template.replaceAll("/\\{[^/]+?\\}", "/([^/]+?)");
        
        if(!str.matches(regex)){
            return;
        }

        String regex2 = template.replaceAll("/\\{[^/]+?\\}", "/(\\\\{[^/]+?\\\\})");

        extractParams(str, regex);
        extractParams(template, regex2);

    }

    private void extractParams(String template, String regex2) {
        Pattern p = Pattern.compile(regex2);
        Matcher m = p.matcher(template);
        m.find();
        for (int i = 1; i <= m.groupCount(); i++) {
            System.out.println(m.group(i));
        }
    }

    @Test
    public void should_test_ClassUtils(){
        Class clazz = getClass();
        Method targetMethod = null;
        for (Method method : clazz.getMethods()) {
            if(method.getName().equals("testPrimitive")){
                targetMethod = method;
                break;
            }
        }
        for (Class<?> aClass : targetMethod.getParameterTypes()) {
            System.out.println(aClass.getName());
        }
        System.out.println(ClassUtils.primitiveToWrapper(new Integer(1).getClass()));
    }

    public int testPrimitive(int i){
        return i;
    }


}